package com.crs.service.impl;

import com.crs.context.BaseContext;
import com.crs.dto.AdminAcceptOrRejectDTO;
import com.crs.entity.Borrow_record;
import com.crs.mapper.AdminMapper;
import com.crs.mapper.BorrowMapper;
import com.crs.mapper.ResourceMapper;
import com.crs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private BorrowMapper borrowMapper;
    /**
     * 管理员同意或拒绝用户申请
     * @param adminAcceptOrRejectDTO
     */
    @Transactional
    @Override
    public void acceptOrReject(AdminAcceptOrRejectDTO adminAcceptOrRejectDTO) {
        Long adminId = BaseContext.getCurrentId();
        adminAcceptOrRejectDTO.setAdminId(adminId);
        //根据借阅记录获取资源的id
        Borrow_record record = borrowMapper.findById(adminAcceptOrRejectDTO.getBorrowId());
        //管理员同意的话，修改资源的状态为已借出
        if(adminAcceptOrRejectDTO.getStatus() == 2){
            resourceMapper.updateStatus(adminAcceptOrRejectDTO.getStatus(), record.getResourceId());
        }
        adminMapper.acceptOrReject(adminAcceptOrRejectDTO);
    }
}
